/**
 * kl yy-ds @2021
 */
package com.cxps.server.order.controller;

import com.cxps.cloud.enums.OrderStatus;
import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.order.domain.OrderDetailsForm;
import com.cxps.server.order.domain.OrderQuery;
import com.cxps.server.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 订单 前端控制器
 *
 * @author YouCai.Liu
 * @since 2021/11/6
 * ...
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单API")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单信息列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "订单信息列表", response = ApiResponse.class)
    public ApiResponse list(@RequestBody OrderQuery query) {
        return ApiResponse.fireSuccess(orderService.list(query));
    }

    /**
     * 订单详细信息
     *
     * @param orderNum
     * @return
     */
    @PostMapping("/details")
    @ApiOperation(value = "订单详细信息", response = ApiResponse.class)
    public ApiResponse details(@RequestParam String orderNum) {
        return ApiResponse.fireSuccess(orderService.details(orderNum));
    }

    /**
     * 保存提交订单
     *
     * @param form
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "提交订单", response = ApiResponse.class)
    public ApiResponse submitOrder(@RequestBody OrderDetailsForm form) {
        try {
            Assert.notNull(form, "提交订单信息对象不能为空");
            Assert.hasLength(form.getVipId(), "会员ID不能为空");
            Assert.notEmpty(form.getOrderDetails(), "订单清单不能为空");
            String orderId = orderService.submitOrder(form);
            return ApiResponse.fireSuccess(orderId);
        } catch (IllegalArgumentException e) {
            log.error("保存提交订单异常：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 根据订单号修改订单的状态
     *
     * @param orderNum
     * @return
     */
    @GetMapping("/updateOrderStatus")
    public ApiResponse updateOrderStatus(@RequestParam String orderNum, @RequestParam OrderStatus orderStatus) {
        orderService.updateOrderStatus(orderNum, orderStatus);
        return ApiResponse.fireSuccess();
    }
}
